package com.qmerak.dao.impl;

import java.util.List;
import java.util.UUID;

import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.qmerak.dao.HibernateDao;
import com.qmerak.entity.ChangeUnionEntity;

/**
 * @author shaohong.cheng
 * @date 2013-6-25
 */
@Repository
public class ChangeUnionDao extends HibernateDao<ChangeUnionEntity, String> {
	public ChangeUnionEntity add(ChangeUnionEntity entity) {
		entity.setId(UUID.randomUUID().toString());
		return super.add(entity);
	}

	@SuppressWarnings("unchecked")
	public ChangeUnionEntity lastChangeUnion(String memberID) {
		Session session = this.sessionFactory.openSession();
		List<ChangeUnionEntity> list = session
				.createQuery(
						"from ChangeUnionEntity where memberID=? order by approveDate")
				.setParameter(0, memberID).list();
		session.close();
		if (!list.isEmpty()) {
			return list.get(0);
		}
		return null;

	}
}
